
var webPath = 'http://www.uniao.me';
var bodyHeight = $('body').height();
bodyHeight *= 0.4;


$(function () {
	// 下载链接
	$('.download').attr('href', webPath + '/share/downLoadPage.htm');
	// 协议地址
	$('.pact-link').attr('href', webPath + '/share/agreement.htm');
	
})
$(window).ready(
	// 框架实现滑动效果
	function() {

		// 点击注册按钮，模态框展示
		$('.register-btn').on('click', function () {
			$('.register-page').css('display','block');
		})
		$('.register-page').on('click', function(event) {
			if (event.target.className == 'register-page') {
				$('.register-page').css('display','none');
			}
		})

		// 点击协议勾选切换 
		$('#pact').on('click', function () {
			if ($(this).attr('checked') == 'true') {
				$(this).attr('checked','false');
				$(this).css('background-image','url(./images/unchecked.png)');
			} else {
				$(this).attr('checked','true');
				$(this).css('background-image','url(./images/checked.png)');
			}
		})

		// 动态验证手机号是否已经注册
		$('#phone').keyup(function(){
			var phone = $('#phone').val(); // 手机号
			if(phone.match(/^(1)[0-9]{10}$/)){
				layer.open({type: 2});
				$.ajax({
					type: 'post',
					url: webPath + '/app/user/checkExist.htm',
					contentType: 'application/json; charset=utf-8',
					data: JSON.stringify({phone: phone}),
					dataType: 'json',
					success: function(data){
						layer.closeAll();
						console.log(data);
						if (data.code == 50006){
							layer.open({
							  	content: '该手机号已经注册',
							  	skin: 'msg',
							  	time: 3,
							  	fixed: false,
							  	top: -bodyHeight
							});
							// 如果手机号已经注册，那么不发送验证码
							$('.verification-btn').off('click', function(){verify(this)});
						} else if(data.code == 200) {
							layer.open({
							  	content: '该手机号未注册',
							  	skin: 'msg',
							  	time: 3,
							  	fixed: false,
							  	top: -bodyHeight
							});
							// 手机符合规则，给获取验证码按钮绑定事件
							$('.verification-btn').on('click', function(){verify(this)});
						}
					},
					error: function(err){
						layer.closeAll();
						alert(err);
					}
				});
			}
		});

		// 获取验证码，验证手机是否已注册
		$('.verification-btn').on('click', function () {
			var phone = $('#phone').val(); // 手机
			if(!phone.match(/^(1)[0-9]{10}$/)){
				layer.open({
				  	content: '手机格式不正确',
				  	skin: 'msg',
				  	time: 3,
					fixed: false,
					top: -bodyHeight
				});
				return false;
			}
		})
		
		// 注册按钮事件验证绑定
		$('#submit').on('click', vilidate);


		// 如果手机号符合规则,可以点击获取验证码 开始倒计时
		function verify(self) {
			var phone = $('#phone').val(); // 手机
			if(!phone.match(/^(1)[0-9]{10}$/)){
				layer.open({
				  	content: '手机格式不正确',
				  	skin: 'msg',
				  	time: 3,
					fixed: false,
					top: -bodyHeight
				});
				return false;
			}
			// ajax请求发送验证码
			$.ajax({
				type: 'post',
				url: webPath + '/common/sms/sendVerify.htm',
				contentType: 'application/json; charset=utf-8',
				data: JSON.stringify({phone: phone, bizType: 0}),
				dataType: 'json',
				success: function(data){
					if (data.code == 200) {
						layer.open({
						  	content: '验证码已发送',
						  	skin: 'msg',
						  	time: 3,
							fixed: false,
							top: -bodyHeight
						})
					}else {
						layer.open({
						  	content: data.message,
						  	skin: 'msg',
						  	time: 3,
							fixed: false,
							top: -bodyHeight
						})
					}
				}
			});
			var time = 60;
			$(self).css('background-color','#f0f0f0');
			$(self).off('click');
			var timer = setInterval(function () {
				if(time == 1){
					time = 60;
					$(self).text('重新发送');
					$(self).css('backgroundColor', '#fff');
					clearInterval(timer);
					$(self).on('click',function(){verify(this)});
				}else{
					time--;
					$(self).text(time + 's后重试');
				}
			},1000);
		}

		// 验证 手机号，密码，验证码，是否勾选协议，并提交
		function vilidate(){
			var phone = $('#phone').val(); // 手机
			var pwd = $('#password').val(); // 密码
			var verify = $('#verification').val(); // 验证码
			var pact_checked = $('#pact').attr('checked'); // 协议
			
			if(!phone.match(/^(1)[0-9]{10}$/)){
				layer.open({
				  	content: '手机格式不正确',
				  	skin: 'msg',
				  	time: 3,
					fixed: false,
					top: -bodyHeight
				})
			}else if($.trim(pwd) == ''){
				layer.open({
				  	content: '请设置登录密码',
				  	skin: 'msg',
				  	time: 3,
					fixed: false,
					top: -bodyHeight
				})
			}else if(!verify){
				layer.open({
				  	content: '请输入验证码',
				  	skin: 'msg',
				  	time: 3,
					fixed: false,
					top: -bodyHeight
				})
			}
			else if(pact_checked == 'false'){
				layer.open({
				  	content: '请勾选协议',
				  	skin: 'msg',
				  	time: 3,
					fixed: false,
					top: -bodyHeight
				})
			}
			else{
				var param = {
					phone: phone,
					password: $.base64.encode(pwd),
					verifyCode: verify,
					reCode: 2000,
					topUserId:2000
				};
				console.log(verify);
				$.ajax({
					type: 'post',
					url: webPath + '/app/user/register.htm',
					contentType: 'application/json; charset=utf-8',
					data: JSON.stringify(param),
					dataType: 'json',
					success: function(data) {
						if(data.code == 200){
							layer.open({
							  	content: '恭喜您，注册成功！2秒后跳转到下载页面',
							  	skin: 'msg',
							  	time: 3,
							  	fixed: false,
							  	top: -bodyHeight
							});
							setTimeout(function() {
								window.location.href = webPath + '/share/downLoadPage.htm?topUserId=2000';
							},2000);
						}else{
							layer.open({
							  	content: data.message,
							  	skin: 'msg',
							  	time: 3,
							  	fixed: false,
							  	top: -bodyHeight
							})
						}
					},
					error: function(data) {
						alert('提交失败');
					}
				});
			}
		}
    }

);



